<?php
namespace Admin\Controller;

use Think\Controller;

class ReportorderController extends RightController
{
    // 加载初始化内容页,并检索
    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_onlineorder");
            $params = session("QG_SEARCH_PARAMS_onlineorder");
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_onlineorder");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_onlineorder", null);
            session("QG_SEARCH_PARAMS_onlineorder", null);
            session("QG_SEARCH_SIZE_onlineorder", null);
            $pageSize = I("post.pageSize");
            $params = I("post.");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }

        // 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 如果是分公司
        if ($this->isCompany()) {
            $where .= " AND sa.companyid = '" . $this->getAccount()['companyid'] . "'";
        }

        // 代理报单
        $where .= " AND t.orderType='3'";

        // 总条数
        $count = M('onlineorder')
            ->alias("t")
            ->join(" LEFT JOIN agen sa ON sa.id = t.SourceId AND t.orderType = 3")
            ->join(" LEFT JOIN agen a ON a.id = t.toagenId")
            ->join(" LEFT JOIN branchcompany b ON b.id = sa.companyid")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('onlineorder')
            ->alias("t")
            ->join(" LEFT JOIN agen sa ON sa.id = t.SourceId AND t.orderType = 3")
            ->join(" LEFT JOIN agen a ON a.id = t.toagenId")
            ->join(" LEFT JOIN branchcompany b ON b.id = sa.companyid")
            ->field("t.*,sa.account as sourceAccount,sa.name as sourceName,a.account as toagenAccount,a.name as toagenName,b.name as companyName")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();

        // 统计
        $sum = M('onlineorder')
            ->alias("t")
            ->join(" LEFT JOIN agen sa ON sa.id = t.SourceId AND t.orderType = 3")
            ->join(" LEFT JOIN agen a ON a.id = t.toagenId")
            ->join(" LEFT JOIN branchcompany b ON b.id = sa.companyid")
            ->field("sum(t.total) as totalSum")
            ->where($where)
            ->order('t.id DESC')
            ->find();

        // 渲染查询条件到session
        $lists = array(
            "page" => getDropdownList("pageSize" , "" ,false),
            "reportStatus" => getDropdownList("reportStatus"),
            "companyList" => $this->getBranchcompanyList()
        );
        session("QG_SEARCH_WHERE_onlineorder", $where);
        session("QG_SEARCH_PARAMS_onlineorder", $params);
        session("QG_SEARCH_SIZE_onlineorder", $pageSize);

        // 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('sum', $sum);
        $this->assign('lists', $lists);
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // 如果是管理
        if ($this->isAdmin() && I('post.companyid')) {
            $where .= " AND sa.companyid = '" . I('post.companyid') . "'";
        }

        // 下单代理
        if (I('post.sourceAccount')) {
            // 精确
            if (I('post.sourceAccountCondition') == 1) {
                $where .= " AND sa.account='" . I("post.sourceAccount") . "'";
            } else {
                $where .= " AND sa.account LIKE '%" . I("post.sourceAccount") . "%'";
            }
        }

        // 单号
        if (I('post.sn')) {
            // 精确
            if (I('post.snCondition') == 1) {
                $where .= " AND t.sn='" . I("post.sn") . "'";
            } else {
                $where .= " AND t.sn LIKE '%" . I("post.sn") . "%'";
            }
        }

        // 上级代理
        if (I('post.toagenAccount')) {
            // 精确
            if (I('post.toagenAccountCondition') == 1) {
                $where .= " AND a.account = '" . I("post.toagenAccount") . "'";
            } else {
                $where .= " AND a.account LIKE '%" . I("post.toagenAccount") . "%'";
            }
        }

        // 状态
        if (I('post.status')) {
            $where .= " AND t.status='" . I("post.status") . "'";
        }

        // 金额
        if (I('post.totalStart') || I('post.totalEnd')) {
            // 开始
            if(I('post.totalStart')){
                $where .= " AND t.total>='" . I("post.totalStart") . "'";
            }
            // 结束
            if(I('post.totalEnd')){
                $where .= " AND t.total<='" . I("post.totalEnd") . "'";
            }
        }

        // 创建时间
        if (I('post.createtimeStart') || I('post.createtimeEnd')) {
            // 起始日期
            if(I('post.createtimeStart')){
                $where .= " AND t.createTime>='" . I("post.createtimeStart") . "'";
            }
            // 结束日期
            if(I('post.createtimeEnd')){
                $where .= " AND t.createTime<='" . I("post.createtimeEnd") . " 23:59:59'";
            }
        }

        // 更新时间
        if (I('post.updatetimeStart') || I('post.updatetimeEnd')) {
            // 起始日期
            if(I('post.updatetimeStart')){
                $where .= " AND t.updateTime>='" . I("post.updatetimeStart") . "'";
            }
            // 结束日期
            if(I('post.updatetimeEnd')){
                $where .= " AND t.updateTime<='" . I("post.updatetimeEnd") . " 23:59:59'";
            }
        }

        return $where;
    }

    // 删除
    public function delete()
    {
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            //删除数据
            $result = M('onlineorder')->where(array("id" => $id))->delete();

            // 如果删除成功
            if ($result) {
                $this->ajaxSuccess("删除成功！");
            } else {
                $this->ajaxError("删除失败！");
            }
        }
    }

    // 详情
    public function info(){
        $id = I("id");
        // 订单详情
        $list = M("orderDetails")
            ->alias("o")
            ->join(" LEFT JOIN product p ON p.id = o.productId")
            ->where(array("orderId" => $id))
            ->field("o.*,p.name as name")
            ->select();

        $this->assign("list" ,$list);
        $this->display();
    }

    // 审核数据
    public function confirm(){
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            M()->startTrans();

            // 审核报单
            $msg = reportReview($id);

            // 如果删除成功
            if (empty($msg)) {
                M()->commit();
                $this->ajaxSuccess("审核成功！");
            } else {
                M()->rollback();
                $this->ajaxError($msg);
            }
        }
    }

    // 驳回
    public function reject(){
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if(empty($id)){
                $this->ajaxError("错误！");
            }

            // 更新状态
            $r[] = M("OnlineOrder")->where(array("id" => $id))->save(array("status"=>"2"));

            // 如果成功
            if (!in_array(false, $r)) {
                M()->commit();
                $this->ajaxSuccess("驳回成功！");
            } else {
                M()->rollback();
                $this->ajaxError("驳回失败！");
            }
        }
    }
}